//
//  TableViewTest.m
//  TestCode
//
//  Created by Clover on 2019/2/25.
//  Copyright © 2019 clover.com. All rights reserved.
//

#import "TableViewTest.h"
#import <MJRefresh/MJRefresh.h>
#import <SDWebImage/UIImageView+WebCache.h>

@interface TableViewTest ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>
@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) MJRefreshGifHeader *mj_gifHeader;
@property (nonatomic, strong) NSArray *gifImageArr;
@end

@implementation TableViewTest
#pragma mark - life
- (void)viewDidLoad {
    [super viewDidLoad];
    _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    [_tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
    _tableview.tableFooterView = [UIView new];
    _tableview.delegate = self;
    _tableview.dataSource = self;
//    _tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(reloadData)];
    _mj_gifHeader = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(reloadData)];
    [_mj_gifHeader setImages:self.gifImageArr forState:MJRefreshStateIdle];
    _tableview.mj_header = _mj_gifHeader;
    
    _tableview.mj_footer = [MJRefreshAutoStateFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerReload)];
    [self.view addSubview:_tableview];
}

#pragma mark - request
- (void)reloadData{
    __weak typeof(self) _self = self;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
         [_self.tableview.mj_header endRefreshing];
    });
}

- (void)footerReload{
    __weak typeof(self) _self = self;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [_self.tableview.mj_footer endRefreshing];
    });
}
#pragma mark - Public Methods

#pragma mark - Private Methods

#pragma mark - Delegate
#pragma mark -- UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

#pragma mark -- UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 12;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1551072081754&di=ca178f4d17ee7131480e1b0b6dee4bb5&imgtype=0&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F01511f57492d286ac72525ae491af2.jpg%40900w_1l_2o_100sh.jpg"] placeholderImage:[UIImage imageNamed:@"image01"]];
    cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
}
#pragma mark - Get && Set
- (NSArray *)gifImageArr{
    if (!_gifImageArr) {
        NSMutableArray *arr = [NSMutableArray array];
        for (int i = 0; i < 5; i ++) {
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"0%d", i + 1]];
            if (image) {
                [arr addObject:image];
            }
        }
        _gifImageArr = [arr copy];
    }
    return _gifImageArr;
}

- (NSString *)title{
    return @"SDWebimage 使用";
}
@end
